package com.engine.modules.ytht.controller;

import com.engine.common.baseEntity.ResponseEntity.ResponseMsg;
import com.engine.modules.ytht.entity.FarmersFieldsEntity;
import com.engine.modules.ytht.entity.PlotInfoEntity;
import com.engine.modules.ytht.mapper.FarmersBindingMapper;
import com.engine.modules.ytht.service.FarmersFieldBindingImpl;
import com.engine.modules.ytht.service.FieldBlockBindingService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName FammerBindingController.java
 * @Description TODO
 * @createTime 2021年12月16日
 */
@RestController
@Api(tags = "2、农户地块绑定")
@ApiSort(value = 3)
@CrossOrigin
@RequestMapping("/FarmerBinding")
@Slf4j
public class FarmerBindingController {

    @Autowired
    FarmersBindingMapper farmersBindingMapper;

    @Autowired
    FarmersFieldBindingImpl farmersFieldBinding;

    @ApiOperation(value = "农户地块绑定", notes = "绑定关系写入，并修改地块图层的suoding字段为1")
    @PostMapping(value = "/FarmerFieldBinding")
    @ApiOperationSupport(order = 1)
    public ResponseMsg FarmerFieldBinding(@RequestBody FarmersFieldsEntity farmersFieldsEntity) {
        try {
            List<String> rs= farmersFieldBinding.updateByFarmersFieldsEntity(farmersFieldsEntity);
            return ResponseMsg.success("数据提交").addData(rs);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error(e.getMessage());
        }
    }

    @ApiOperation(value = "农户地块解绑绑定", notes = "绑定关系删除，同时检查该地块是否还有其他绑定关系，如果都没有则地块图层的suoding字段设为0")
    @PostMapping(value = "/FarmerFieldUnbinding")
    @ApiOperationSupport(order = 2)
    public ResponseMsg FarmerFieldUnbinding(@RequestBody FarmersFieldsEntity farmersFieldsEntity) {
        try {
            List<String> rs= farmersFieldBinding.deleteByFarmersFields(farmersFieldsEntity);
            return ResponseMsg.success("成功解绑").addData(rs);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询绑定关系", notes = "查询绑定关系")
    @GetMapping(value = "/GetFarmerFieldBinding/{id_card}/{year}")
    @ApiOperationSupport(order = 3)
    public ResponseMsg GetFarmerFieldBinding(@PathVariable String id_card,
                                             @PathVariable Long year) {
        try {
            FarmersFieldsEntity rs= farmersFieldBinding.seleteFarmersFieldsEntityByIdCard(id_card,year);
            return ResponseMsg.success("查询成功").addData(rs);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error(e.getMessage());
        }
    }

}
